<style lang="scss">
  .login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100vh;
    background: url(../../assets/images/bg1.jpg) top left no-repeat,
      url(../../assets/images/bg2.jpg) top right no-repeat,
      url(../../assets/images/bg3.jpg) bottom left no-repeat,
      url(../../assets/images/bg4.jpg) bottom right no-repeat;
    background-size: 50% 50%;
    .login-box {
      opacity: 0.88;
      overflow: hidden;
      .login-form {
        width: 480px;
        background: #ffffff;
        padding: 20px 0 1px 0;
        border-radius: 8px;
        // box-shadow: 7px 4px 48px #72B6FC;
        .login-title {
          height: 32px;
          line-height: 32px;
          margin-bottom: 12px;
          color: #72b6fc;
          font-size: 16px;
          font-weight: bold;
          text-align: center;
          letter-spacing: 2px;
        }
      }
      .el-row {
        margin-bottom: 20px;
      }
    }
  }
</style>

<template>
  <div class="login-container"
       v-loading="loadSubmitting">
    <div class="login-box"
         @keyup.enter="doLogin">
      <el-form class="login-form">
        <p class="login-title">基层治理和社会事业局网络理政平台</p>
        <el-row>
          <el-col :span="18"
                  :offset="3">
            <el-input v-model="username"
                      placeholder="请输入帐号">
              <template slot="prepend">帐号</template>
            </el-input>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18"
                  :offset="3">
            <el-input v-model="password"
                      type="password"
                      placeholder="请输入密码">
              <template slot="prepend">密码</template>
            </el-input>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
                  :offset="6">
            <el-button type="primary"
                       :loading="loadSubmitting"
                       v-on:click="doLogin"
                       style="width:100%">登 录</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        username: process.env.NODE_ENV === 'development' ? 'chenyy' : '',
        password: process.env.NODE_ENV === 'development' ? 'A@lamp2020' : '',
        loadSubmitting: false
      }
    },
    methods: {
      // 登录方法
      doLogin () {
        if (this.username === '') {
          this.$message({ message: '请输入用户名', type: 'warning' })
          return
        }
        if (this.password === '') {
          this.$message({ message: '请输入密码', type: 'warning' })
          return
        }

        this.loadSubmitting = true

        // 发送登录请求
        this.$axios.post(this.$api.common.login, {
          userName: this.username,
          userPwd: this.$md(this.password)
        })
        .then((rs) => {
          if (rs) {
            // 将 userInfo 存入 sessionStorage
            sessionStorage.setItem('GOVERNANCE_USER_INFO', JSON.stringify(rs))
            // 登录成功 跳转 main 页面
            this.$message({ message: '登录成功', type: 'success', showClose: true })
            this.$router.push(JSON.parse(sessionStorage.getItem('GOVERNANCE_USER_INFO')).menuEntityList[0].path)
          } else {
            this.loadSubmitting = false
          }
        })
      },
      // 会话id登录
      doHhidLogin () {
        const { hhid } = this.$route.query
        if (hhid) {
          this.loadSubmitting = true
          this.$axios.get(this.$api.common.loginWithToken + '/' + hhid).then(rs => {
            // console.log(rs);
            // 将 userInfo 存入 sessionStorage
            sessionStorage.setItem('GOVERNANCE_USER_INFO', JSON.stringify(rs))
            this.$router.push(JSON.parse(sessionStorage.getItem('GOVERNANCE_USER_INFO')).menuEntityList[0].path)
          }).catch(() => {
            this.$message.warning('系统未给你授权，请联系管理员')
            this.$router.push('/login')
          })
        }
      }
    },
    mounted () {
      this.doHhidLogin()
    }
  }
</script>
